<!--
 * @Author: C.
 * @Date: 2023-03-07 14:04:14
-->
<template>
  <Upload
    :multiple="false"
    name="file"
    :customRequest="beforeUpload"
    :show-upload-list="false"
    class="upload-modal-toolbar__btn"
  >
    <slot></slot>
  </Upload>
</template>

<script lang="ts" setup name="JUpload">
  import { Upload } from 'ant-design-vue';

  import { uploadImg, uploadFile } from '/@/api/work/system/file';
  import { propTypes } from '/@/utils/propTypes';

  const emit = defineEmits(['success']);
  const props = defineProps({
    type: propTypes.string.def('file'),
  });
  // 上传前校验
  function beforeUpload(file) {
    // console.log(file.file);
    let formData = new FormData();
    let uploadApi = props.type == 'image' ? uploadImg : uploadFile;
    console.log(file.file, 'file.file');

    formData.append('file', file.file);
    uploadApi(formData).then((res) => {
      // console.log(res, 666);
      emit('success', res[0]);
    });
  }
</script>
<style lang="less" scoped></style>
